package fun.coding.leetcode;

public class RemoveDuplicatesFromSortedList {

	public static void main(String[] args) {

	}
	
	/**
		Given 1->1->2, return 1->2.
		Given 1->1->2->3->3, return 1->2->3.
	 */
	public ListNode deleteDuplicates(ListNode head) {
		ListNode n1 = head;
		ListNode n2 = head;
		
		while (n2 != null) {
			while (n2 != null && n2.val == n1.val) {
				n2 = n2.next;
			}
			// bypass the duplicate node. JVM GC will take care of those orphaned nodes.  
			n1.next = n2;
			n1 = n2;
		}
		return head;
	}

}
